function checkPerBox(){
	if(document.getElementsByName("cbIds") == null) {
		return;
	}
	if(document.getElementsByName("cbIds").length == 1){
		if (document.getElementsByName("cbIds")[0].checked){
			document.getElementById("checkAll").checked = true;
		}else{
			document.getElementById("checkAll").checked = false;
		}
	}else{
		var chkCnt = 0;
		for(var i=0;i<document.getElementsByName("cbIds").length;i++){
			if(document.getElementsByName("cbIds")[i].checked) {
				chkCnt++;
			}
		}
		if(chkCnt == document.getElementsByName("cbIds").length){
			document.getElementById("checkAll").checked = true;
		}else{
			document.getElementById("checkAll").checked = false;
		}
	}
}

function checkAllOrNot(){
	if(document.getElementsByName("cbIds") == null) {
		return;
	}

	if(document.getElementById("checkAll").checked){
		if(document.getElementsByName("cbIds").length == null){
			document.getElementsByName("cbIds").checked = true;
		}else{
			for(var i=0;i<document.getElementsByName("cbIds").length;i++){
				document.getElementsByName("cbIds")[i].checked = true;
			}
		}
	}else {
		if(document.getElementsByName("cbIds").length == null){
			document.getElementsByName("cbIds").checked = false;
		}else{
			for(var i=0;i<document.getElementsByName("cbIds").length;i++){
				document.getElementsByName("cbIds")[i].checked = false;
			}
		}
	}
}

function checkAtLeastOne(){
	var flag = false;
	
	if(document.getElementsByName("cbIds").length == null){
		flag = document.getElementsByName("cbIds").checked;
	}else{
		for(var i=0;i<document.getElementsByName("cbIds").length;i++){
			if(document.getElementsByName("cbIds")[i].checked){
				flag = true;
				break;
			}
		}
	}

	return flag;
}

function checkOnlyOne(){
	var flag = false;
	
	if(document.getElementsByName("cbIds").length == 1){
		flag = document.getElementsByName("cbIds")[0].checked;
	}else{
		var count = 0;
		for(var i=0;i<document.getElementsByName("cbIds").length;i++){
			if(document.getElementsByName("cbIds")[i].checked){
				count++;
			}
		}
		if(count == 1){
			flag = true;
		}
	}

	return flag;
}

function doDelete(){
	if(document.getElementsByName("cbIds") == null){
		return;
	}
	
	if(!checkAtLeastOne()){
		alert("请至少选择一条记录");
		return;
	}

	if (document.getElementsByName("cbIds").length == 1){
		document.getElementById("keys").value = document.getElementsByName("cbIds")[0].value;
	}else{
		document.getElementById("keys").value = "";
		for(var i=0;i<document.getElementsByName("cbIds").length;i++){
			if(document.getElementsByName("cbIds")[i].checked){
				document.getElementById("keys").value += "," + document.getElementsByName("cbIds")[i].value;
			}
		}
		document.getElementById("keys").value = document.getElementById("keys").value.trimComma();//substring(1)将去除第一个逗号
	}
	if(confirm("确认删除吗？")){
		document.getElementById("formDelete").submit();
	}
}

function doEdit(){
	if(document.getElementsByName("cbIds") == null){
		return;
	}
	
	if(!checkAtLeastOne()){
		alert("请至少选择一条记录");
		return;
	}
	
	if(!checkOnlyOne()){
		alert("请只选择一条记录");
		return;
	}

	if (document.getElementsByName("cbIds").length == 1){
		document.getElementById("updatekey").value = document.getElementsByName("cbIds")[0].value;
	}else{
		for(var i=0;i<document.getElementsByName("cbIds").length;i++){
			if(document.getElementsByName("cbIds")[i].checked){
				document.getElementById("updatekey").value = document.getElementsByName("cbIds")[i].value;
				break;
			}
		}
	}
	
	document.getElementById("formEdit").submit();
}

function doAdd(){
	document.getElementById("formAdd").submit();
}

function doExport(){
	document.getElementById("formExport").submit();
}

function checkPerBox1(chIdsName, checkAllId){
	if(document.getElementsByName(chIdsName) == null) {
		return;
	}
	if(document.getElementsByName(chIdsName).length == 1){
		if (document.getElementsByName(chIdsName)[0].checked){
			document.getElementById(checkAllId).checked = true;
		}else{
			document.getElementById(checkAllId).checked = false;
		}
	}else{
		var chkCnt = 0;
		for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
			if(document.getElementsByName(chIdsName)[i].checked) {
				chkCnt++;
			}
		}
		if(chkCnt == document.getElementsByName(chIdsName).length){
			document.getElementById(checkAllId).checked = true;
		}else{
			document.getElementById(checkAllId).checked = false;
		}
	}
}

function checkAllOrNot1(chIdsName, checkAllId){
	if(document.getElementsByName(chIdsName) == null) {
		return;
	}

	if(document.getElementById(checkAllId).checked){
		if(document.getElementsByName(chIdsName).length == null){
			document.getElementsByName(chIdsName).checked = true;
		}else{
			for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
				document.getElementsByName(chIdsName)[i].checked = true;
			}
		}
	}else {
		if(document.getElementsByName(chIdsName).length == null){
			document.getElementsByName(chIdsName).checked = false;
		}else{
			for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
				document.getElementsByName(chIdsName)[i].checked = false;
			}
		}
	}
}

function checkAtLeastOne1(chIdsName){
	var flag = false;
	
	if(document.getElementsByName(chIdsName).length == null){
		flag = document.getElementsByName(chIdsName).checked;
	}else{
		for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
			if(document.getElementsByName(chIdsName)[i].checked){
				flag = true;
				break;
			}
		}
	}

	return flag;
}

function checkOnlyOne1(chIdsName){
	var flag = false;
	
	if(document.getElementsByName(chIdsName).length == 1){
		flag = document.getElementsByName(chIdsName)[0].checked;
	}else{
		var count = 0;
		for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
			if(document.getElementsByName(chIdsName)[i].checked){
				count++;
			}
		}
		if(count == 1){
			flag = true;
		}
	}

	return flag;
}

function doDelete1(chIdsName, deleteKeysId, formDeleteId){
	if(document.getElementsByName(chIdsName) == null){
		return;
	}
	
	if(!checkAtLeastOne1(chIdsName)){
		alert("请至少选择一条记录");
		return;
	}

	if (document.getElementsByName(chIdsName).length == 1){
		document.getElementById(deleteKeysId).value = document.getElementsByName(chIdsName)[0].value;
	}else{
		document.getElementById(deleteKeysId).value = "";
		for(var i=0;i<document.getElementsByName(chIdsName).length;i++){
			if(document.getElementsByName(chIdsName)[i].checked){
				document.getElementById(deleteKeysId).value += "," + document.getElementsByName(chIdsName)[i].value;
			}
		}
		document.getElementById(deleteKeysId).value = document.getElementById(deleteKeysId).value.trimComma();//substring(1)将去除第一个逗号
	}
	if(confirm("确认删除吗？")){
		document.getElementById(formDeleteId).submit();
	}
}

/* 取得href中url的参数，本方法仅适用于一个参数 */
function getUrlParamOfHref(jqHrefObj) {
	var urlHref = jqHrefObj.attr("href");
	return urlHref.substring(urlHref.indexOf("=")+1);
}

/* 取得href上包含在id中url参数，本方法仅适用于一个参数 */
function getUrlParamOfHrefId(jqHrefObj) {
	var urlHref = jqHrefObj.attr("id");
	return urlHref.substring(urlHref.indexOf("=")+1);
}

/* 删除左右两端的空格：该函数运用于非Jquery环境下的JS一般调用 */
String.prototype.trim = function() {
  var str = this,
  str = str.replace(/^\s\s*/, ''),
  ws = /\s/,
  i = str.length;
  while (ws.test(str.charAt(--i)));
  return str.slice(0, i + 1);
}

/* 删除左右两端的逗号 */
String.prototype.trimComma = function() {
  var str = this;
 	if (str.length != 0) {
 		if (str.substring(0,1) == ",") {
 			str = str.substring(1);
 		}
  	if (str.length != 0) {
   	var lastIndex = str.length-1;
  		if (str.substring(lastIndex) == ",") {
  			str = str.substring(0, lastIndex);
  		}
  	}		   		
 	}
  return str;
}